package nc.tb.excel.ctl;

import jxl.Cell;
import jxl.Range;
import nc.tb.cube.cell.AreaPosition;

public class JXLExcelSheet implements IExcelSheet {
	
	private jxl.Sheet sheet = null;

	public JXLExcelSheet(jxl.Sheet _sheet) {
		sheet = _sheet;
	}

	public int getFirstRowNum() {
		return sheet == null? -1: 0;
	}

	public int getLastRowNum() {
		return sheet == null? -1: sheet.getRows() - 1;
	}

	public int getNumMergedRegions() {
		Range[] cells = sheet.getMergedCells();
		return cells == null ? 0 : cells.length;
	}

	public AreaPosition getAreaPositionFromMergedRegionAt(int idx) {
		Range[] cells = sheet.getMergedCells();
		if (cells == null || cells.length-1 < idx)
			return null;
		return new AreaPosition(cells[idx].getTopLeft().getRow(), cells[idx].getTopLeft().getColumn(),
				cells[idx].getBottomRight().getRow(), cells[idx].getBottomRight().getColumn());
	}
	
	public short getRowFirstCellNum(int rowIdx) {
		Cell[] row = sheet.getRow(rowIdx);
		return row == null ? (short)-1 : 0;
	}
	
	public short getRowLastCellNum(int rowIdx) {
		Cell[] row = sheet.getRow(rowIdx);
		return row == null ? (short)-1 : (short)(row.length - 1);
	}
	
	public boolean hasRow(int rowIdx) {
		return sheet.getRow(rowIdx) != null;
	}

}
